# Create your views here.
from random import randint

from django_redis import get_redis_connection
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from meiduo_mall.libs.yuntongxun.sms import CCP
from celery_tasks.sms.tasks import send_sms_code

class SMSCodeView(APIView):
    def get(self,request,mobile):
        conn = get_redis_connection("verifications")
        flag = conn.get("sms_flag_%s"%mobile)
        if flag:
            return Response({"message": "发送短信过于频繁"},status=status.HTTP_400_BAD_REQUEST)

        sms_code = "%06d"%randint(0,999999)

        pl = conn.pipeline()

        pl.setex("sms_code_%s"%mobile,300,sms_code)
        pl.setex("sms_flag_%s"%mobile,60,1)
        pl.execute()

        # ccp = CCP()
        # ccp.send_template_sms(mobile,[sms_code,'5'],1)
        send_sms_code.delay(mobile, sms_code)
        return Response({'message':'ok'})
